$(function(){
	reloadVerifyCode('getImageCode');
	var showImageCode = $("#showImageCode").val();
	if(showImageCode == 'true'){
		$("#divImageCode").show();
		reloadVerifyCode('getImageCode');
	} else {
		$("#divImageCode").hide();
	}
})
var InterValObj;// timer变量，控制时间
var curCount = 60;// 当前剩余秒数
function getcode() {
	var mobileResult = checkMobile($("#mobile").val());
	if (!mobileResult.result) {
		myalert(mobileResult.msg);
		return false;
	}
	var imagecode = "";
	if(!$("#divImageCode").is(':hidden')){
		imagecode = $("#imagecode").val();
		if(imagecode == '' || imagecode == '请输入验证码'){
			myalert('请输入图形验证码');
			return false;
		}
	}
	showLoading();
	$.ajax({
		type : "get",
		url : appPath + "/member/sendCode.html",
		data : {
			tid : new Date().getTime(),
			loginname : $("#mobile").val(),
			imagecode : imagecode
		},
		success : function(result) {
			hideLoading();
			myalert(result.msg);
			if (result.code == '1') {
				curCount = 60;
				InterValObj = window.setInterval(setRemainTime, 1000);
			} else if (result.code == '-2') {
				curCount = result.second;
				InterValObj = window.setInterval(setRemainTime, 1000);
			}
			if(result.showImageCode == '1'){
				$("#divImageCode").show();
			} else {
				$("#divImageCode").hide();
			}
			if(result.msg.indexOf("图形验证码错误")>-1){
				reloadVerifyCode('getImageCode');
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			hideLoading();
			myalert("系统异常，请稍后再试");
		}
	});
}
// 验证码有效期倒计时
function setRemainTime() {
	if (curCount == 0) {
		window.clearInterval(InterValObj);// 停止计时器
	} else {
		curCount--;
	}
}

function reglogin() {
	var mobile = $("#mobile").val();
	if (mobile == '请输入手机号') {
		myalert('手机号不能为空');
		return false;
	}
	var mobileResult = checkMobile(mobile);
	if (!mobileResult.result) {
		myalert(mobileResult.msg);
		return false;
	}
	if ($("#code").val() == '' || $("#code").val() == '请输入验证码') {
		myalert("验证码不能为空");
		return false;
	}
	showLoading();
	$.ajax({
		type : "post",
		url : appPath + "/wechat/regloginsubmit.html",
		data : {
			mobile : $("#mobile").val(),
			code : $("#code").val(),
			type : $("#type").val()
		},
		success : function(result) {
			hideLoading();
			if (result.code == '1') {
				$.cookie("token", result.token, {
					expires : 365,
					path : '/',
					domain : '.xinghaoshangcheng.com'
				});
				// 本地测试需要加上端口号，另外修改hosts文件例如本机192.168.6.18 abcd123.xinghaoshangcheng.com
				// location.href = result1.domain + ":8080" + appPath + "/center/center.htm?token=" + result1.token;
				location.href = result.domain + "/center/center.htm?token=" + result.token;
			} else {
				myalert(result.msg);
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			hideLoading();
			myalert("系统异常，请稍后再试");
		}
	});
}